package com.getpebble.android.common.util;

import android.content.Context;
import com.getpebble.android.common.core.trace.Trace;
import java.io.File;
import java.util.concurrent.TimeUnit;

public class FileUtil {
    private static final long KEEN_FILE_DELETION_CUTOFF_PERIOD_MILLIS = TimeUnit.DAYS.toMillis(30);
    private static final String TAG = FileUtil.class.getSimpleName();

    public static abstract class FileFoundCallback {
        public abstract void directoryFound(File file);

        public abstract void fileFound(File file);
    }

    public static void walk(File root, FileFoundCallback callback) {
        File[] list = root.listFiles();
        if (list == null) {
            Trace.warning(TAG, "null list to walk: " + root);
            return;
        }
        for (File f : list) {
            if (f != null) {
                if (f.isDirectory()) {
                    callback.directoryFound(f);
                    walk(f, callback);
                } else {
                    callback.fileFound(f);
                }
            }
        }
    }

    public static void purgeTemporaryFiles(Context context) {
        if (context == null) {
            Trace.info(TAG, "purgeTemporaryFiles: context is null");
            return;
        }
        try {
            purgeFilesRecursively(context.getExternalFilesDir(null));
        } catch (NullPointerException e) {
            Trace.warning(TAG, "NPE getting attempting to purge files from external files dir");
        } catch (ArrayIndexOutOfBoundsException a) {
            Trace.warning(TAG, "ArrayOutOfBounds attempting to purge files from external dir", a);
        }
        purgeFilesRecursively(context.getFilesDir());
        purgeFilesRecursively(context.getCacheDir());
    }

    static void purgeFilesRecursively(File dir) {
        if (dir == null) {
            Trace.warning(TAG, "null dir to purge");
            return;
        }
        Trace.debug(TAG, "walking.. " + dir.toString());
        try {
            walk(dir, new FileFoundCallback() {
                public void directoryFound(File file) {
                }

                public void fileFound(File file) {
                    boolean shouldDelete = (file.getAbsoluteFile().toString().contains("keen") && file.lastModified() < System.currentTimeMillis() - FileUtil.KEEN_FILE_DELETION_CUTOFF_PERIOD_MILLIS) || file.getName().endsWith(".pbw") || file.getName().endsWith(".log") || file.getName().endsWith(".gz") || file.getName().endsWith(".bin");
                    if (shouldDelete) {
                        Trace.verbose(FileUtil.TAG, "deleting... " + file.getPath());
                        file.delete();
                    }
                }
            });
        } catch (Throwable e) {
            Trace.error(TAG, e);
        }
    }

    public static boolean delete(File file) {
        try {
            return file.delete();
        } catch (Exception e) {
            return false;
        }
    }
}
